package com.nwu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nwu.entities.conferencePaperForm;
import com.nwu.entities.vo.ConferencePaperQuery;
import com.nwu.service.ConferencePaperManagementService;
import com.nwu.util.Result;
import com.nwu.util.resultCode.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 增删改查接口
 *  * @author Li hz
 *  * @since 2022-04-29
 * </p>
 */

@Api(tags = "会议论文管理")
@RestController
@RequestMapping("/ResearchProjectCollectionSpringBoot/getConferencePaperInfo")
public class ConferencePaperManagementController {
    @Autowired
    private ConferencePaperManagementService conferencePaperManagementService;
    //查询所有信息

    @ApiOperation("查询所有论文信息")
    @GetMapping("findAll")
    public R findAllInfo() {
        List<conferencePaperForm> list = conferencePaperManagementService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation("逻辑删除论文信息")
    @DeleteMapping("{id}")
    public R removePaper(@PathVariable Long id) {
        boolean flag = conferencePaperManagementService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页查询          current 当前页，limit记录数
    @ApiOperation("分页查询论文信息")
    @GetMapping("pageFind/{current}/{limit}")
    public R pageList(@PathVariable long current,
                      @PathVariable long limit) {
        //创建page对象
        Page<conferencePaperForm> pageSciResearch = new Page<>(current, limit);
        conferencePaperManagementService.page(pageSciResearch, null);
        long total = pageSciResearch.getTotal();   //总记录数
        List<conferencePaperForm> records = pageSciResearch.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    //条件查询带分页
    @ApiOperation("带条件的分页查询论文信息")
    @PostMapping("pageConditionFind/{current}/{limit}")
    public R pageConditionFind(@PathVariable long current, @PathVariable long limit,
                               @RequestBody(required = false) ConferencePaperQuery conferencePaperQuery) {

        //创建page对象，传递当前页和每页记录数
        Page<conferencePaperForm> pageSciResearch = new Page<>(current, limit);
        QueryWrapper<conferencePaperForm> wrapper = new QueryWrapper<>();
        //wrapper构建条件,多条件组合查询 动态sql，判断条件是否为空，如果不为空拼接条件
        String paper_name = conferencePaperQuery.getPaper_name();
        String author_id = conferencePaperQuery.getAuthor_id();
        String conference_name = conferencePaperQuery.getConference_name();
        String organizer = conferencePaperQuery.getOrganizer();
        String paper_date =conferencePaperQuery.getPaper_date();
        String periodical_language =conferencePaperQuery.getPeriodical_language();
        if (!StringUtils.isEmpty(paper_name)) {
            wrapper.like("paper_name", paper_name);
        }
        if (!StringUtils.isEmpty(author_id)) {
            wrapper.eq("author_id", author_id);
        }
        if (!StringUtils.isEmpty(conference_name)) {
            wrapper.like("conference_name", conference_name);
        }
        if (!StringUtils.isEmpty(organizer)) {
            wrapper.eq("organizer", organizer);
        }
        if (!StringUtils.isEmpty(paper_date)) {
            wrapper.eq("paper_date",paper_date);
        }
        if (!StringUtils.isEmpty(periodical_language)) {
            wrapper.eq("periodical_language",periodical_language);
        }

        conferencePaperManagementService.page(pageSciResearch, wrapper);

        long total = pageSciResearch.getTotal();   //总记录数
        List<conferencePaperForm> records = pageSciResearch.getRecords(); //数据list集合

        return R.ok().data("total", total).data("rows", records);

    }

    //教师添加论文信息
    @ApiOperation("添加论文信息")
    @PostMapping("addConferencePaperManagement")
    public R addSciManagement(@RequestBody conferencePaperForm conferencePaperForm) {
        conferencePaperForm.setIsDeleted(0);
        boolean save = conferencePaperManagementService.save(conferencePaperForm);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    //根据科研项目id进行查询
    @ApiOperation("根据论文id查询")
    @GetMapping("GetConferencePaper/{paper_id}")
    public String getThesis(@PathVariable String paper_id) {
        conferencePaperForm paper = conferencePaperManagementService.getById(paper_id);
        return Result.ok("paper", paper);
    }

    //科研信息修改
    @ApiOperation("论文信息修改")
    @PostMapping("updateConferencePaper")
    public R updateThesis(@RequestBody conferencePaperForm conferencePaperForm) {
        boolean flag = conferencePaperManagementService.updateById(conferencePaperForm);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}